package com.rocksdata.mc.database;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.rocksdata.mc.entity.StudentInfo;
import com.rocksdata.mc.entity.StudentContactInfo;
import com.rocksdata.mc.entity.TeacherContactInfo;
import com.rocksdata.mc.entity.TeacherInfo;
import com.rocksdata.mc.entity.UserInfo;

public class UserInfoDBManager{
	private static UserInfoDBManager instance = null;
	
	private UserInfoDBManager(){
	}
	
	static synchronized UserInfoDBManager getInstance(){
		if(instance == null){
			instance = new UserInfoDBManager();
		}
		return instance;
	}

	public synchronized void saveStudentContactList(List<StudentContactInfo> contactList) {
		if(contactList == null || contactList.size() == 0) return;
		SQLiteDatabase db = DatabaseManager.getDatabase();
		db.beginTransaction(); 
		try {     
            for (StudentContactInfo parent: contactList) {
            	for(StudentInfo info: parent.getStudents()){
            		ContentValues values = new ContentValues();    
            		values.put(DatabaseHelper.TUserInfo.USER_ID, info.getStudentCode());
            		values.put(DatabaseHelper.TUserInfo.USER_NAME, info.getStudentName());
            		values.put(DatabaseHelper.TUserInfo.USER_TYPE, info.getIsTeacher());
            		values.put(DatabaseHelper.TUserInfo.PARENT_ID, parent.getClassCode());
            		values.put(DatabaseHelper.TUserInfo.PARENT_NAME, parent.getClassName());

                    db.replace(DatabaseHelper.TUserInfo.TABLE_NAME, null, values);
            	}
            }

            db.setTransactionSuccessful();     
        } catch (Exception e) {     
            e.printStackTrace();     
        } finally {     
            db.endTransaction();     
        } 
	}

	public synchronized void saveTeacherContactList(List<TeacherContactInfo> contactList) {
		if(contactList == null || contactList.size() == 0) return;
		SQLiteDatabase db = DatabaseManager.getDatabase();
		db.beginTransaction();
		try {
			for (TeacherContactInfo parent: contactList) {
				for(TeacherInfo info: parent.getStaffs()){
					ContentValues values = new ContentValues();
					values.put(DatabaseHelper.TUserInfo.USER_ID, info.getStaffCode());
					values.put(DatabaseHelper.TUserInfo.USER_NAME, info.getStaffName());
					values.put(DatabaseHelper.TUserInfo.USER_TYPE, info.getIsTeacher());
					values.put(DatabaseHelper.TUserInfo.PARENT_ID, parent.getDepartmentCode());
					values.put(DatabaseHelper.TUserInfo.PARENT_NAME, parent.getDepartmentName());

					db.replace(DatabaseHelper.TUserInfo.TABLE_NAME, null, values);
				}
			}

            db.setTransactionSuccessful();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			db.endTransaction();
		}
	}

	public synchronized List<StudentContactInfo> getStudentContactList() {
		Cursor c = DatabaseManager.getDatabase().query(DatabaseHelper.TUserInfo.TABLE_NAME, null,
				DatabaseHelper.TUserInfo.USER_TYPE + "=?",
				new String[]{String.valueOf(UserInfo.USER_TYPE_STUDENT)},
				null, null, null);
		List<StudentContactInfo> parentList = new ArrayList<>();
		while(c.moveToNext()){
			String userId = c.getString(c.getColumnIndex(DatabaseHelper.TUserInfo.USER_ID));
			String userName = c.getString(c.getColumnIndex(DatabaseHelper.TUserInfo.USER_NAME));
			String parentId = c.getString(c.getColumnIndex(DatabaseHelper.TUserInfo.PARENT_ID));
			String parentName = c.getString(c.getColumnIndex(DatabaseHelper.TUserInfo.PARENT_NAME));

			StudentContactInfo parentInfo = new StudentContactInfo();
			parentInfo.setClassCode(parentId);
			parentInfo.setClassName(parentName);

			StudentInfo childInfo = new StudentInfo();
			childInfo.setStudentCode(userId);
			childInfo.setStudentName(userName);
			StudentContactInfo classInfo = new StudentContactInfo();
			classInfo.setClassCode(parentId);
			classInfo.setClassName(parentName);
			childInfo.setStudentClass(classInfo);
			childInfo.setIsTeacher(UserInfo.USER_TYPE_STUDENT);
			
			if(!parentList.contains(parentInfo)){
				parentList.add(parentInfo);
			}else{
                for(StudentContactInfo info: parentList){
                    if(parentInfo.equals(info)){
                        parentInfo = info;
                        break;
                    }
                }
            }

			if(parentInfo.getStudents() == null){
				parentInfo.setStudents(new ArrayList<StudentInfo>());
			}

            parentInfo.getStudents().add(childInfo)	;

		}
		c.close();
		if(parentList.size() == 0) return null;
		return parentList;
	}

	public synchronized List<TeacherContactInfo> getTeacherContactList() {
		Cursor c = DatabaseManager.getDatabase().query(DatabaseHelper.TUserInfo.TABLE_NAME, null,
				DatabaseHelper.TUserInfo.USER_TYPE + "=?",
				new String[]{String.valueOf(UserInfo.USER_TYPE_TEACHER)},
				null, null, null);
		List<TeacherContactInfo> parentList = new ArrayList<>();
		while(c.moveToNext()){
			String userId = c.getString(c.getColumnIndex(DatabaseHelper.TUserInfo.USER_ID));
			String userName = c.getString(c.getColumnIndex(DatabaseHelper.TUserInfo.USER_NAME));
			String parentId = c.getString(c.getColumnIndex(DatabaseHelper.TUserInfo.PARENT_ID));
			String parentName = c.getString(c.getColumnIndex(DatabaseHelper.TUserInfo.PARENT_NAME));

			TeacherContactInfo parentInfo = new TeacherContactInfo();
			parentInfo.setDepartmentCode(parentId);
			parentInfo.setDepartmentName(parentName);

			TeacherInfo childInfo = new TeacherInfo();
			childInfo.setStaffCode(userId);
			childInfo.setStaffName(userName);
			childInfo.setIsTeacher(UserInfo.USER_TYPE_TEACHER);
			TeacherContactInfo departmentinfo = new TeacherContactInfo();
			departmentinfo.setDepartmentCode(parentId);
			departmentinfo.setDepartmentName(parentName);
            childInfo.setDepartment(departmentinfo);

			if(!parentList.contains(parentInfo)){
				parentList.add(parentInfo);
			}else{
                for(TeacherContactInfo info: parentList){
                    if(parentInfo.equals(info)){
                        parentInfo = info;
                        break;
                    }
                }
            }

			if(parentInfo.getStaffs() == null){
				parentInfo.setStaffs(new ArrayList<TeacherInfo>());
			}

            parentInfo.getStaffs().add(childInfo);
		}
		c.close();
		if(parentList.size() == 0) {
            return null;
        }
		return parentList;
	}

	public synchronized void saveTeacherDetail(TeacherInfo info){
        if(info == null || info.getStaffCode() == null) return;
        ContentValues cv = new ContentValues();
        cv.put(DatabaseHelper.TUserInfo.HEAD_URL, info.getAvatar());
        cv.put(DatabaseHelper.TUserInfo.MOBILE, info.getMobile());
        cv.put(DatabaseHelper.TUserInfo.QR_CODE, info.getQrcode());

        DatabaseManager.getDatabase().update(DatabaseHelper.TUserInfo.TABLE_NAME, cv,
                DatabaseHelper.TUserInfo.USER_ID + "=?",
                new String[]{info.getStaffCode()});
    }

	public synchronized TeacherInfo getTeacherInfo(String userId){
		Cursor c = DatabaseManager.getDatabase().query(DatabaseHelper.TUserInfo.TABLE_NAME, null,
				DatabaseHelper.TUserInfo.USER_ID + "=?",
				new String[]{userId},
				null, null, null);
		if(c.moveToFirst()){
            TeacherInfo info = new TeacherInfo();
            info.setStaffCode(userId);
			info.setStaffName(c.getString(c.getColumnIndex(DatabaseHelper.TUserInfo.USER_NAME)));
			info.setIsTeacher(UserInfo.USER_TYPE_STUDENT);
			info.setAvatar(c.getString(c.getColumnIndex(DatabaseHelper.TUserInfo.HEAD_URL)));
			info.setMobile(c.getString(c.getColumnIndex(DatabaseHelper.TUserInfo.MOBILE)));
            TeacherContactInfo departmentinfo = new TeacherContactInfo();
			departmentinfo.setDepartmentCode(c.getString(c.getColumnIndex(DatabaseHelper.TUserInfo.PARENT_ID)));
			departmentinfo.setDepartmentName(c.getString(c.getColumnIndex(DatabaseHelper.TUserInfo.PARENT_NAME)));
			info.setDepartment(departmentinfo);

			c.close();
            return info;
		}

		c.close();
		return null;
    }

    public synchronized void saveStudentDetail(StudentInfo info){
        if(info == null || info.getStudentCode() == null) return;
        ContentValues cv = new ContentValues();
        cv.put(DatabaseHelper.TUserInfo.HEAD_URL, info.getAvatar());
        cv.put(DatabaseHelper.TUserInfo.MOBILE, info.getMobile());
        cv.put(DatabaseHelper.TUserInfo.QR_CODE, info.getQrcode());
		cv.put(DatabaseHelper.TUserInfo.BEDROOM_NAME, info.getBedroom_num());

        DatabaseManager.getDatabase().update(DatabaseHelper.TUserInfo.TABLE_NAME, cv,
                DatabaseHelper.TUserInfo.USER_ID + "=?",
                new String[]{info.getStudentCode()});
    }

    public synchronized StudentInfo getStudentInfo(String userId){
        Cursor c = DatabaseManager.getDatabase().query(DatabaseHelper.TUserInfo.TABLE_NAME, null,
                DatabaseHelper.TUserInfo.USER_ID + "=?",
                new String[]{userId},
                null, null, null);
        if(c.moveToFirst()){
            StudentInfo info = new StudentInfo();
            info.setStudentCode(userId);
            info.setStudentName(c.getString(c.getColumnIndex(DatabaseHelper.TUserInfo.USER_NAME)));
            info.setIsTeacher(UserInfo.USER_TYPE_STUDENT);
            info.setAvatar(c.getString(c.getColumnIndex(DatabaseHelper.TUserInfo.HEAD_URL)));
            info.setMobile(c.getString(c.getColumnIndex(DatabaseHelper.TUserInfo.MOBILE)));
			StudentContactInfo classInfo = new StudentContactInfo();
			classInfo.setClassCode(c.getString(c.getColumnIndex(DatabaseHelper.TUserInfo.PARENT_ID)));
			classInfo.setClassName(c.getString(c.getColumnIndex(DatabaseHelper.TUserInfo.PARENT_NAME)));
            info.setStudentClass(classInfo);
			info.setBedroom_num(c.getString(c.getColumnIndex(DatabaseHelper.TUserInfo.BEDROOM_NAME)));

            c.close();
            return info;
        }
		c.close();
        return null;
    }

    public synchronized UserInfo getUserBaseInfo(String userId){
		Cursor c = DatabaseManager.getDatabase().query(DatabaseHelper.TUserInfo.TABLE_NAME, null,
				DatabaseHelper.TUserInfo.USER_ID + "=?",
				new String[]{userId},
				null, null, null);
		if(c.moveToFirst()){
			UserInfo info = new UserInfo();
			info.setUserID(userId);
			info.setUserName(c.getString(c.getColumnIndex(DatabaseHelper.TUserInfo.USER_NAME)));
			info.setIsTeacher(UserInfo.USER_TYPE_STUDENT);
			info.setAvatar(c.getString(c.getColumnIndex(DatabaseHelper.TUserInfo.HEAD_URL)));

			c.close();
			return info;
		}
		c.close();
		return null;
	}

	public synchronized List<StudentInfo> getStudentListByClassNum(String class_num){
		Cursor c = DatabaseManager.getDatabase().query(DatabaseHelper.TUserInfo.TABLE_NAME, null,
				DatabaseHelper.TUserInfo.PARENT_ID + "=?",
				new String[]{String.valueOf(class_num)},
				null, null, null);
		List<StudentInfo> list = new ArrayList<>();
		while(c.moveToNext()){
			StudentInfo info = new StudentInfo();
			info.setStudentCode(c.getString(c.getColumnIndex(DatabaseHelper.TUserInfo.USER_ID)));
			info.setStudentName(c.getString(c.getColumnIndex(DatabaseHelper.TUserInfo.USER_NAME)));
			info.setIsTeacher(c.getInt(c.getColumnIndex(DatabaseHelper.TUserInfo.USER_TYPE)));

			list.add(info);
		}
		c.close();
		return list;
	}

}
